package com.malou.client.seguridad.usuario;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;

public class GrupoDialog extends Dialog {
	
	private HiddenField<String> id;
	public HiddenField<String> parent_id;
	protected TextField<String> usuario;
	private Button cancelar;
	private Button guardar;	
	public Status status;
	public UsuarioPanel usuarioPanel;
	private FormBinding formBindings;
	Usuario user;
	
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public GrupoDialog(){
		setLayout(new FitLayout());
		setModal(true);
		setPlain(true);
		
	    setSize(280, 110);
	    setClosable(true);
	    
	    FormPanel panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields(panel);
	    
	    setButtonAlign(HorizontalAlignment.LEFT);
	    setButtons("");
	    add(panel);
	    
	    setBodyBorder(true);	    
	    setResizable(false);	    
	    setFocusWidget(usuario);
	}
	
	protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar());
	    
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  GrupoDialog.this.hide();
	    	  Usuarios.store.rejectChanges();
	      }

	    });

	    guardar = new Button(constants.guardar());
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	  if(validate()){
	    		status.show();
	  		    getButtonBar().disable();
	  		    
	  			user = new Usuario();
	  			if(id.getValue() != null)
	  				user.setId(Integer.valueOf(id.getValue()));
	  			if(parent_id.getValue() != null)
	  				user.setParent_id(Integer.valueOf(parent_id.getValue()));
	  			user.setUsuario(usuario.getValue());
	  			 				
	  			Usuarios.grupoDialog = GrupoDialog.this;
	  			Usuarios.saveGroup(user);
	    	  }	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	}
	
	private void addFields(ContentPanel cp) {

		id = new HiddenField<String>();
		id.setVisible(false);
		cp.add(id);
		
		parent_id = new HiddenField<String>();
		parent_id.setVisible(false);
		cp.add(parent_id);
				
		usuario = new TextField<String>();
		usuario.setMinLength(3);
		usuario.setFieldLabel(constants.usuarioGrupo());	    
		usuario.setAllowBlank(false);	    
		cp.add(usuario, new FormData("-20"));

	}
	
	public void beforeAdd(){
		setIconStyle("icon-group");
		setHeading(constants.usuarioNuevoGrupo());
	}
	
	public void beforeEdit(){
		setIconStyle("icon-update");
		setHeading(constants.usuarioModificarGrupo());		
		status.hide();
		getButtonBar().enable();
	}
	
	public void reset(){		  		  
		id.clear();
		usuario.clear();		
		parent_id.clear();		
		status.hide();
		getButtonBar().enable();
	}
	
	public void setStore(TreeStore<Usuario> store){
		  
		formBindings.addFieldBinding(new FieldBinding(id, "id"));
		formBindings.addFieldBinding(new FieldBinding(parent_id, "parent_id"));
		formBindings.addFieldBinding(new FieldBinding(usuario, "usuario"));
		  
		formBindings.setStore(store);
	}
	
	protected boolean validate() {
		boolean gr = usuario.isValid();		
		return gr;
	}
	
	public void bind(Usuario selectedModel){		  
		formBindings.bind(selectedModel);
	}
	public void unbind(){
		formBindings.unbind();
	}
}
